using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework;

namespace SPGE
{
    public class Button : GameItem, ITouchUpdatable
    {
        public override Rectangle SourceRectangle
        {
            get
            {
                if (!IsButtonEnabled)
                    return DisabledSourceRectangle;
                else if (IsPressed)
                    return PressedSourceRectangle;
                else
                    return DefaultSourceRectangle;
            }
            set { }
        }

        private TimeSpan remainingTime;
        public TimeSpan DepressTime
        { get; set; }

        public bool IsToggle
        { get; set; }

        public override string Path
        { get; protected set; }

        public virtual Rectangle PressedSourceRectangle
        { get; set; }
        public virtual Rectangle DefaultSourceRectangle
        { get; set; }

        public bool IsPressed { get; set; }
        private bool? isEnabled = null;
        public bool IsButtonEnabled 
        {
            get { return isEnabled ?? true; }
            set { isEnabled = value; }
        }

        public Action OnPressed { get; set; }
        public Action OnToBePressed { get; set; }

        public SimpleText Text
        { get; set; }

        public Button(string path)
            : base(SpriteBatchUse.AlreadyBeganSpriteBatch)
        {
            Path = path;
            IsPressed = false;
            IsToggle = false;

            DepressTime = TimeSpan.FromSeconds(0.125);
        }

        public override void Initialize()
        {
            IsPressed = false;
        }

        public override void Load(ResourceGroupHandler group)
        {
            if (Text != null)
                Text.Load(group);

            base.Load(group);
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime, TouchCollection collection, List<GestureSample> gestures)
        {
            if(!IsToggle && IsPressed)
            {
                remainingTime -= gameTime.ElapsedGameTime;

                if (remainingTime <= TimeSpan.Zero)
                {
                    remainingTime = TimeSpan.Zero;
                    IsPressed = false;

                    if (OnPressed != null)
                        OnPressed();
                }
            }

            foreach (var gesture in gestures)
                if (gesture.GestureType == GestureType.Tap
                    && ContainedArea.Contains(new Point((int)gesture.Position.X, (int)gesture.Position.Y)))
                    ButtonClicked();
        }

        /// <summary>
        /// Handle all flags here.
        /// </summary>
        protected virtual void ButtonClicked()
        {
            if (OnToBePressed != null)
                OnToBePressed();

            if (DepressTime > TimeSpan.Zero)
                remainingTime = DepressTime;

            if (IsToggle)
                IsPressed = !IsPressed;
            else
                IsPressed = true;

            if (IsToggle && OnPressed != null)
                OnPressed();
        }

        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (Text != null)
            {
                var xPos = Position.X - (Size.X * Origin.X);
                var yPos = Position.Y - (Size.Y * Origin.Y);
                Text.Position = new Vector2((2 * xPos + Size.X) / 2f, (2 * yPos + Size.Y) / 2f);
                Text.Origin = Vector2.One / 2f;

                Text.RenderLevel = this.RenderLevel - 0.00001f;
                Text.Draw(gameTime, spriteBatch);
            }
            base.Draw(gameTime, spriteBatch);
        }
    }
}
